package com.xyz.controller;

import cn.hutool.core.date.DateUtil;
import com.xyz.aop.Param;
import com.xyz.config.SystemData;
import com.xyz.controller.vo.NFCVo;
import com.xyz.controller.vo.ShipFlagVO;
import com.xyz.dto.PatrolTaskStateListDto;
import com.xyz.entity.mapper.EquipmentTreeMapper;
import com.xyz.entity.vo.EquipmentInfoExportVo;
import com.xyz.entity.vo.PatrolWorkOrderExportVo;
import com.xyz.service.EquipmentService;
import com.xyz.service.PatrolService;
import com.xyz.service.RoleService;
import com.xyz.util.BeanUtils;
import com.xyz.util.ExcelUtils;
import com.xyz.util.WordTemplateUtil;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.GridData;
import com.xyz.util.dto.LoginInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("patrol")
public class PatrolController {

    private static final Logger logger = LoggerFactory.getLogger(PatrolController.class);

    @Autowired
    private PatrolService patrolService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private RoleService roleService;

    @PostMapping("selectPatrol")
    public DataResult selectPatrol(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(isRequired = false) String state,
            @Param(isRequired = false, type = Param.ParamType.POSITIVE_INTEGER) String worker,
            @Param(isRequired = false) String treeId
    ) {
        try {
            return patrolService.selectPatrol(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), state, worker, treeId);
        } catch (Exception e) {
            logger.error("selectPatrol", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("exportData")
    public DataResult exportData(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(isRequired = false) String state,
            @Param(isRequired = false, type = Param.ParamType.POSITIVE_INTEGER) String worker,
            @Param(isRequired = false) String treeId,
            HttpServletResponse response
    ) {
        try {
            DataResult dataResult = patrolService.selectPatrol(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), state, worker, treeId);
            Object obj = dataResult.getInfo();
            List<PatrolWorkOrderExportVo> personList = new ArrayList<>();
            if (obj != null && obj instanceof GridData) {
                GridData gridData = (GridData) obj;
                personList = BeanUtils.toBean(gridData.getRows(), PatrolWorkOrderExportVo.class);

                for (PatrolWorkOrderExportVo equipmentInfoExportVo : personList) {
                    if (equipmentInfoExportVo.getTreeId() != null) {
                        String treeName = equipmentService.regionName(equipmentInfoExportVo.getTreeId());
                        equipmentInfoExportVo.setTreeName(treeName);
                    }
                    if (equipmentInfoExportVo.getPatrolEmployeeId() == 0) {
                        equipmentInfoExportVo.setPatrolEmployee("");
                    }else {
                        String employeeName = SystemData.employeeMap.get(equipmentInfoExportVo.getPatrolEmployeeId()).getEmployeeName();
                        equipmentInfoExportVo.setPatrolEmployee(employeeName);
                    }
                }
            }
            ExcelUtils.write(response, "巡检列表.xls", "巡检列表", PatrolWorkOrderExportVo.class,
                    personList);
            return DataResult.build9100();
        } catch (Exception e) {
            logger.error("selectPatrol", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectPatrolApp")
    public DataResult selectPatrolApp(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(isRequired = false) String state,
            @Param(isRequired = false, type = Param.ParamType.POSITIVE_INTEGER) String worker,
            @Param(isRequired = false) String treeId,
            @Param(isRequired = false) String orderCode,
            @Param(isRequired = false) String strTime,
            @Param(isRequired = false) String endTime
    ) {
        try {
            return patrolService.selectPatrolApp(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), state, worker, treeId, orderCode, strTime,endTime);
        } catch (Exception e) {
            logger.error("selectPatrolApp", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectPatrolOne")
    public DataResult selectPatrolOne(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.selectPatrolOne(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectPatrolOne", e);
            return DataResult.build9500();
        }
    }


    @PostMapping("updateSkipFlag")
    public DataResult updateSkipFlag(
            LoginInfo li,
            @RequestBody ShipFlagVO shipFlagVO
    ) {
        try {
            return patrolService.updateSkipFlag(li, shipFlagVO);
        } catch (Exception e) {
            logger.error("updateSkipFlag", e);
            return DataResult.build9500();
        }
    }


    @PostMapping("selectNFCPatrolOne")
    public DataResult selectNFCPatrolOne(
            LoginInfo li,
            @RequestBody NFCVo nfcVo
    ) {
        try {
            return patrolService.selectNFCPatrolOne(li, nfcVo);
        } catch (Exception e) {
            logger.error("selectPatrolOne", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertPatrol")
    public synchronized DataResult insertPatrol(
            LoginInfo li,
            @Param String startDay,
            @Param String startTime,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String treeId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String patrolEmployeeId
    ) {
        try {
            return patrolService.insertPatrol(li, startDay, startTime, Long.parseLong(treeId), Long.parseLong(patrolEmployeeId));
        } catch (Exception e) {
            logger.error("insertPatrol", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updatePatrol")
    public DataResult updatePatrol(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param String startDay,
            @Param String startTime,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String treeId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String patrolEmployeeId
    ) {
        try {
            return patrolService.updatePatrol(li, Long.parseLong(id), startDay, startTime, Long.parseLong(treeId), Long.parseLong(patrolEmployeeId));
        } catch (Exception e) {
            logger.error("updatePatrol", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("deletePatrol")
    public DataResult deletePatrol(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.deletePatrol(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("deletePatrol", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectPatrolTask")
    public DataResult selectPatrolTask(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.selectPatrolTask(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectPatrolTask", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectPatrolTaskPreview")
    public DataResult selectPatrolTaskPreview(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.selectPatrolTaskPreview(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectPatrolTaskPreview", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectPatrolPlan")
    public DataResult selectPatrolPlan(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize,
            @Param(isRequired = false) String treeId,
            @Param(isRequired = false) String businessType,
            @Param(isRequired = false) String state
    ) {
        try {
            return patrolService.selectPatrolPlan(li, Integer.parseInt(pageNum), Integer.parseInt(pageSize), treeId, businessType, state);
        } catch (Exception e) {
            logger.error("selectPatrolPlan", e);
            return DataResult.build9500();
        }
    }


    @PostMapping("selectPatrolPlanOne")
    public DataResult selectPatrolPlanOne(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.selectPatrolPlanOne(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectPatrolPlanOne", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("insertPatrolPlan")
    public DataResult insertPatrolPlan(
            LoginInfo li,
            @Param String planName,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String businessType,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String dateInterval,
            @Param(type = Param.ParamType.DATE, rule = "yyyy-MM-dd") String planStartTime,
            @Param(type = Param.ParamType.DATE, rule = "yyyy-MM-dd") String planEndTime,
            @Param(type = Param.ParamType.DATE, rule = "yyyy-MM-dd HH:mm:ss") String firstTime,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String treeId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String teamGroup
    ) {
        try {
            return patrolService.insertPatrolPlan(li, planName,
                    Integer.parseInt(businessType), Integer.parseInt(dateInterval),
                    DateUtil.parseDate(planStartTime), DateUtil.parseDate(planEndTime),
                    DateUtil.parseDateTime(firstTime), Long.parseLong(treeId),teamGroup);
        } catch (Exception e) {
            logger.error("insertPatrolPlan", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updatePatrolPlan")
    public DataResult updatePatrolPlan(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param String planName,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String businessType,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String dateInterval,
            @Param(type = Param.ParamType.DATE, rule = "yyyy-MM-dd") String planStartTime,
            @Param(type = Param.ParamType.DATE, rule = "yyyy-MM-dd") String planEndTime,
            @Param(type = Param.ParamType.DATE, rule = "yyyy-MM-dd HH:mm:ss") String firstTime,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String treeId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String state,
            @Param String teamGroup
    ) {
        try {
            return patrolService.updatePatrolPlan(li, Long.parseLong(id), planName,
                    Integer.parseInt(businessType), Integer.parseInt(dateInterval),
                    DateUtil.parseDate(planStartTime), DateUtil.parseDate(planEndTime),
                    DateUtil.parseDateTime(firstTime), Long.parseLong(treeId), Integer.parseInt(state),teamGroup);
        } catch (Exception e) {
            logger.error("updatePatrolPlan", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("deletePatrolPlan")
    public DataResult deletePatrolPlan(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.deletePatrolPlan(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("deletePatrolPlan", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectPatrolAnnounce")
    public DataResult selectPatrolAnnounce(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.selectPatrolAnnounce(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectPatrolAnnounce", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectPatrolFlow")
    public DataResult selectPatrolFlow(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.selectPatrolFlow(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectPatrolFlow", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectPatrolDetail")
    public DataResult selectPatrolDetail(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.selectPatrolDetail(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectPatrolDetail", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectPatrolDispose")
    public DataResult selectPatrolDispose(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.selectPatrolDispose(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectPatrolDispose", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("selectPatrolAccept")
    public DataResult selectPatrolAccept(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.selectPatrolAccept(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("selectPatrolAccept", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updatePatrolState")
    public DataResult updatePatrolState(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param String operation,
            @Param(isRequired = false, type = Param.ParamType.POSITIVE_INTEGER) String acceptState,
            @Param(isRequired = false) String operationInfo,
            @Param(isRequired = false) String acceptScore
    ) {
        try {
            return patrolService.updatePatrolState(li, Long.parseLong(id), Integer.parseInt(operation), operationInfo, acceptScore, acceptState);
        } catch (Exception e) {
            logger.error("updatePatrolState", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updatePatrolTaskState")
    public DataResult updatePatrolTaskState(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String state

    ) {
        try {
            return patrolService.updatePatrolTaskState(li, Long.parseLong(id), Integer.parseInt(state));
        } catch (Exception e) {
            logger.error("updatePatrolTaskState", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updatePatrolTaskStateList")
    public DataResult updatePatrolTaskStateList(
            LoginInfo li,
            @RequestBody List<PatrolTaskStateListDto> dtoList
    ) {
        try {
            return patrolService.updatePatrolTaskStateList(li, dtoList);
        } catch (Exception e) {
            logger.error("updatePatrolTaskState", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updatePatrolTask")
    public DataResult updatePatrolTask(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String state,
            @Param(isRequired = false) String recordedValue
    ) {
        try {
            return patrolService.updatePatrolTask(li, Long.parseLong(id), Integer.parseInt(state), recordedValue);
        } catch (Exception e) {
            logger.error("updatePatrolTask", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updatePatrolDetail")
    public DataResult updatePatrolDetail(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(isRequired = false) String orderException,
            @Param(isRequired = false) String filePicCode,
            @Param(isRequired = false) String fileAudioCode,
            @Param(isRequired = false) String fileVideoCode
    ) {
        try {
            return patrolService.updatePatrolDetail(li, Long.parseLong(id), orderException, filePicCode, fileAudioCode, fileVideoCode);
        } catch (Exception e) {
            logger.error("updatePatrolDetail", e);
            return DataResult.build9500();
        }
    }

    @PostMapping("updatePatrolDispose")
    public DataResult updatePatrolDispose(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(isRequired = false, type = Param.ParamType.POSITIVE_INTEGER) String faultType,
            @Param(isRequired = false, type = Param.ParamType.POSITIVE_INTEGER) String faultReason,
            @Param(isRequired = false, type = Param.ParamType.POSITIVE_INTEGER) String emState,
            @Param(isRequired = false) String disposeResult,
            @Param(isRequired = false) String fileOldPicCode,
            @Param(isRequired = false) String fileOldAudioCode,
            @Param(isRequired = false) String fileOldVideoCode,
            @Param(isRequired = false) String fileNewPicCode,
            @Param(isRequired = false) String fileNewAudioCode,
            @Param(isRequired = false) String fileNewVideoCode
    ) {
        try {
            return patrolService.updatePatrolDispose(
                    li, Long.parseLong(id), faultType,
                    faultReason, emState, disposeResult,
                    fileOldPicCode, fileOldAudioCode, fileOldVideoCode,
                    fileNewPicCode, fileNewAudioCode, fileNewVideoCode
            );
        } catch (Exception e) {
            logger.error("updatePatrolDispose", e);
            return DataResult.build9500();
        }
    }

    /*
     * 领取工单
     * */
    @PostMapping("/receivePatrol")
    public DataResult receivePatrol(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.receivePatrol(loginInfo, id);
        } catch (Exception e) {
            logger.error("receivePatrol", e);
            return DataResult.build9500();
        }
    }

    /*
     * 巡检工单转交
     * */
    @PostMapping("/transferPatrol")
    public DataResult transferPatrol(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.transferPatrol(loginInfo, id);
        } catch (Exception e) {
            logger.error("transferPatrol", e);
            return DataResult.build9500();
        }
    }

    /*
     * 工单作废
     * */
    @PostMapping("/nullifyPatrol")
    public DataResult nullifyPatrol(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return patrolService.nullifyPatrol(loginInfo, id);
        } catch (Exception e) {
            logger.error("nullifyPatrol", e);
            return DataResult.build9500();
        }
    }

    /*
     * 巡检导出
     * */
    @GetMapping("/getInfoWord")
    public void getInfoWord(LoginInfo loginInfo,@RequestParam String id,
                            HttpServletResponse response) throws Exception {
        Map<String, Object> dataMap = patrolService.getInfoWord(loginInfo,id);
        WordTemplateUtil.exportWord(response, dataMap, "xj.docx");
    }
}
